package io.coderyeah.ymcc.service.impl;

import io.coderyeah.ymcc.constant.YmccConstants;
import io.coderyeah.ymcc.domain.CourseType;
import io.coderyeah.ymcc.mapper.CourseTypeMapper;
import io.coderyeah.ymcc.service.ICourseTypeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程目录 服务实现类
 * </p>
 *
 * @author coderyeah
 * @since 2022-10-23
 */
@Service
@Slf4j
public class CourseTypeServiceImpl extends ServiceImpl<CourseTypeMapper, CourseType> implements ICourseTypeService {
    @Autowired
    RedisTemplate<Object, Object> redisTemplate;

    @Cacheable(cacheNames = YmccConstants.CACHE_COURSE_TYPE, key = "'all'")
    @Override
    public List<CourseType> treeData() {
       /* List<CourseType> list = null;
        final Object o = redisTemplate.opsForValue().get(YmccConstants.CACHE_COURSE_TYPE);
        if (null != o) {
            // 从redis中读取返回数据
            list = (List<CourseType>) o;
            System.out.println("-------redis");
        } else {
            list = getCourseTypes();
            // 存入redis
            redisTemplate.opsForValue().set(YmccConstants.CACHE_COURSE_TYPE, list);
            System.out.println("-------mysql");
        }*/
        log.debug("=============查询了数据库============");
        return getCourseTypes();
    }

    // 从数据库中查询
    private List<CourseType> getCourseTypes() {
        // 查询所有分类
        List<CourseType> courseTypes = selectList(null);
        // 将集合转换为map
        Map<Long, CourseType> map = courseTypes.stream().collect(Collectors.toMap(CourseType::getId, courseType -> courseType));
        // 返回给前端的集合
        List<CourseType> list = new ArrayList<>();
        // 遍历
        courseTypes.forEach(courseType -> {
            if (courseType.getPid() == null || courseType.getPid() == 0) {
                // 顶级
                list.add(courseType);
            } else {
                // 找到父级
                CourseType type = map.get(courseType.getPid());
                if (type != null) {
                    type.getChildren().add(courseType);
                }
            }
        });
        return list;
    }


    // 剔除缓存
    @CacheEvict(cacheNames = YmccConstants.CACHE_COURSE_TYPE, key = "'all'")
    @Override
    public boolean insert(CourseType entity) {
        return super.insert(entity);
    }

    @CacheEvict(cacheNames = YmccConstants.CACHE_COURSE_TYPE, key = "'all'")
    @Override
    public boolean deleteById(Serializable id) {
        return super.deleteById(id);
    }

    @CacheEvict(cacheNames = YmccConstants.CACHE_COURSE_TYPE, key = "'all'")
    @Override
    public boolean updateById(CourseType entity) {
        return super.updateById(entity);
    }
}
